package cn.edu.controller;

import cn.edu.service.StockService;
import cn.edu.service.StockServiceLock;
import cn.edu.service.StockServiceRedis;
import cn.edu.service.StockServiceRedisson;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Mr.ma
 * @since 2022/12/8 16:06
 **/
@RestController
@RequestMapping("/stock")
public class StockController {
    @Resource
    private StockService stockService;
    @Resource
    private StockServiceLock stockServiceLock;
    @Resource
    private StockServiceRedis stockServiceRedis;
    @Resource
    private StockServiceRedisson stockServiceRedisson;

    @RequestMapping("decrement")
    public String decrement(Integer id){
        int stock = stockService.decrement(id);
        // 'if' statement can be collapsed
        if (stock > 0){
            return "修改库存数量成功，编号为" + id + "的商品剩余库存为：" + stock;
        }
        return "编号为" + id + "的商品剩余库存不足";
    }

    @RequestMapping("decLock")
    public String decLock(Integer id){
        return stockServiceLock.decrement(id);
    }

    @RequestMapping("decRedis")
    public String decRedis(Integer id){
        return stockServiceRedis.decrement(id);
    }

    @RequestMapping("decRedisson")
    public String decRedisson(Integer id){
        return stockServiceRedisson.decrement(id);
    }
}
